package Communication;

import java.io.IOException;
import java.util.ArrayList;
import Controller.*;
import Entity.Category;
import Entity.CommentReport;
import Entity.Customer;
import Entity.Report;
import Entity.Type;
import Entity.Worker;

public class Client extends AbstractClient{



	
	public Worker currentWorker;
	public Customer currentCustomer;
	public Category currentCategory;
	public String currentWorkerId;
	
	ArrayList<String> cityList;
	 ArrayList<Report> reportsList;
	ArrayList<Category> categoriesList;
	ArrayList<Type> TypesList;
	String[] listOfCities ;
	 static CommentReport Cmt;
	

	String str;

	final public static int DEFAULT_PORT = 5555;

	/**
	 * Make connection between the client and the database .
	 * @param host host
	 * @param port port
	 * @throws IOException
	 */
	public Client(String host, int port) throws IOException {
		super(host, port);
		openConnection();
		System.out.println("Client is connected !");
		currentWorker = new Worker();
		currentCustomer = new Customer();
		cityList = new ArrayList<String>();
		categoriesList = new ArrayList<Category>();
		currentCategory = new Category();
		
	}

	
	
	/**Handles a message sent from the server to this client. 
	 * This MUST be implemented by subclasses, who should respond to * messages.
	 * @param msg the message sent.
	 */
	protected void handleMessageFromServer(Object msg) {

		MessageHandle Original = (MessageHandle) msg;
		Object data;

		
		switch (Original.getType()) 
		{

		case MessageHandle.WORKER:
			data = (Worker) Original.getData();
			setCurrentWorker((Worker) data);

			break;
		
		case MessageHandle.ADD_CUSTOMER:			
	        str = (String) Original.getData();
			AddNewCustomerController.updataView(str  );
			break;	
			
		case MessageHandle.UPDATE_GET_CUSTOMER:
			UpdateCustomerController.UpdateView(Original); 
		
			break;
		case MessageHandle.CUSTOMER:
			data = (Customer) Original.getData();
			setCurrentCustomer((Customer) data);
			break;
	
		case MessageHandle.SALE:
			 str = (String) Original.getData();
			RunSaleController.updataView(str);
			break;
			
		case MessageHandle.PRODUCT:
			 str = (String) Original.getData();
			NewProductToCatalogController.updataView(str);
			break;
			
		case MessageHandle.CATEGORY:
	        str = (String) Original.getData();
			SettingDistributionsController.updataView(str  );
			break;	

		case MessageHandle.GET_CITIES:
			Entity.Cities.setCities((String [])Original.getData());
			break;	
			
		case MessageHandle.GET_CUSTOMERS:
			Entity.CustomerCard.setCustomers((String [])Original.getData());
			break;	
			
		case MessageHandle.ADD_CITY:
			 str = (String) Original.getData();
				HandleCityController.updataView(str  );
			break;
			
		case MessageHandle.DELETE_CITY:
			 str = (String) Original.getData();
				HandleCityController.updataView(str  );
			break;	

		case MessageHandle.PATTERN:
	        str = (String) Original.getData();
			PatternController.updataView(str );
			break;
			
		case MessageHandle.COMMENT_REPORT:
			 str = (String) Original.getData();
			CommentsReportController.updataView(str);
			break;
			
		case MessageHandle.CUSTOMER_REPORT:
			 str = (String) Original.getData();
			ReportsController.updataView(str);
			break;
			
		case MessageHandle.PATTERN_FOR_SALE:
			 str = (String) Original.getData();
			 RunSaleController.updateView1(str);
			break;
			
		case MessageHandle.UPDATE_GET_CUSTOMER_HISTORY:
			 SearchCustomerHistoryController.UpdateView(Original);
			break;
			
		case MessageHandle.GET_PRODUCTS:
			Entity.Product.setProducts((String [])Original.getData());
			break;
		}}


	/**
	 * 
	 * @param worker
	 */
	public void setCurrentWorker(Worker worker){
		currentWorker = worker;
	}
   /**
    * 
    * @param customer
    */
	public void setCurrentCustomer(Customer customer){
		currentCustomer = customer;
	}
	/**
	 * 
	 * @param category
	 */
	public void setCurrentCategory(Category category){
		currentCategory = category;
	}
	/**
	 * 
	 * @param WorkerId
	 */
	public void setCurrentWorkerID(String WorkerId) {
		currentWorkerId = WorkerId;
	}
	
	/**
	 * @return currentUser
	 */
	public Worker getCurrentWorker() {
		return currentWorker;
	}
	/**
	 * 
	 * @return currentCustomer
	 */
	public Customer getCurrentCustomer() {
		return currentCustomer;
	}
	/**
	 * 
	 * @return currentWorkerID
	 */
	public String getCurrentWorkerID() {
		return currentWorker.getWorkerID();
	}
	/**
	 * 
	 * @return currentCategory
	 */
	public Category getCurrentCategory() {
		return currentCategory;
	}
	/**
	   * Closes the connection to the server.
	   *
	   * @exception IOException if an I/O error occurs when closing.
	   */
	public void quit() {
		try {
			closeConnection();
		} catch (IOException e) {
		}
		System.exit(0);
	}
/**
 * The function sent the MessageHandle to the server
 * @param query
 * @param type
 * @param updateDB (if its true =update the database if false= don't update the database)
 * @throws IOException
 */
	public void getData(String query, int type, boolean updateDB) {

		try {
			MessageHandle msg = new MessageHandle(query, type, updateDB);
			sendToServer(msg);
		} catch (IOException e) {
			e.printStackTrace();
			quit();
		}
	}

}
	